﻿/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2005  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg
{
	import nanoxml.XMLElement;
	import com.dg.gaming.api.common.GameOver;
	import com.dg.gaming.api.common.IGameOver;
	import com.dg.gaming.api.common.util.DGUtils;

	/**
	 * Communication object which describes a game over.
	 *
	 * @author Bob Marks
	 * @version Alpha 0.2.3
	 */
	public class MsgGameOver extends MsgTableMessage
	{
		// Fields
		private var gameOver:GameOver=null;

		private static const XML_ATT_PLAYERS:String="players";
		private static const XML_ATT_RESULTS:String="results";
		private static const XML_ATT_OLD_RATINGS:String="oldRatings";
		private static const XML_ATT_NEW_RATINGS:String="newRatings";



		/**
		 * Constructor which reads the game over object from an XML element.
		 *
		 * @param message                     XML element version of object.
		 */
		public function MsgGameOver(resultType:int=-1, tableNum:String=null, gameOver:GameOver=null, message:XMLElement=null)
		{

			setTableNum(tableNum);
			this.gameOver=gameOver;
			if (message != null)
				super(message);

			if (message == null)
			{
				return;
			}
			// Retrieve info from XML.
			var playersAtt:String=message.getStringAttribute(XML_ATT_PLAYERS);
			var resultsAtt:String=message.getStringAttribute(XML_ATT_RESULTS);
			var oldRtngAtt:String=message.getStringAttribute(XML_ATT_OLD_RATINGS);
			var newRtngAtt:String=message.getStringAttribute(XML_ATT_NEW_RATINGS);

			// If these attributes are OK then create a game over object.
			if (playersAtt != null && resultsAtt != null && oldRtngAtt != null && newRtngAtt != null)
			{
				var players:Array=DGUtils.convertToIntArray(playersAtt);
				var results:Array=DGUtils.convertToIntArray(resultsAtt);
				var oldRatings:Array=DGUtils.convertToIntArray(oldRtngAtt);
				var newRatings:Array=DGUtils.convertToIntArray(newRtngAtt);

				// Create game over object
				this.gameOver=new GameOver(players, results, oldRatings, newRatings);
			}
		}

		/**
		 * Return the score.  If its a draw this will probobly be 0, a win a
		 * positive number and a negative number depending on the game.
		 *
		 * @return score increment.
		 */
		public function getGameOver():GameOver
		{
			return this.gameOver;
		}

		/**
		 * Flatten communciation object.
		 *
		 * @see com.dg.gaming.api.common.msg.ITransmittable#flatten()
		 */
		public override function flatten():XMLElement
		{
			var message:XMLElement=flattenWithName(Msg.GAME_OVER);

			// Populate message
			if (gameOver != null)
			{
				message.setAttribute(XML_ATT_PLAYERS, DGUtils.valueOfArray(gameOver.getPlayers()));
				message.setAttribute(XML_ATT_RESULTS, DGUtils.valueOfArray(gameOver.getResults()));
				message.setAttribute(XML_ATT_OLD_RATINGS, DGUtils.valueOfArray(gameOver.getOldRatings()));
				message.setAttribute(XML_ATT_NEW_RATINGS, DGUtils.valueOfArray(gameOver.getNewRatings()));
			}

			return message; // flatten XML to a String
		}
	}
}